﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MaLu.Data;
using MaLu.Common;
using MaLu.BusiLogic;
using System.Text;
using MaLu.Framework.Common;

namespace MaluJg.web.PovertyIn
{
    public partial class UStudy : SUserControl
    {
        public void SaveData()
        {
            
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("declare @id int; set @id={0}; select @id=id from SStudy where id=@id;", RecordID);
            sb.AppendLine("if @@rowcount =0 begin");
            sb.AppendFormat(@"INSERT INTO [SStudy]
           ([Name],[StudentName],[Gender],[IDNum],[Household],[BeLongTo],[Address]
           ,[School] ,[Money],[Year],[HalfYear])
     VALUES
           ({0},{1},'{2}',{3},'{4}','{5}',{6},{7},{8},{9},'{10}'); end else begin

UPDATE [SStudy]
   SET [Name] ={0}   ,[StudentName]={1}  ,[Gender] = '{2}',[IDNum]={3},[Household]='{4}'
      ,[BeLongTo] = '{5}'     ,[Address] =  {6}
      ,[School] =  {7} ,[Money]={8},[Year] = {9}
      ,[HalfYear] =  '{10}' WHERE id=@id ; end", Util.ReturnSqlValue(tbName.Text), Util.ReturnSqlValue(tbStuName.Text), ddlGendar.SelectedValue,
                    Util.ReturnSqlValue(tbIDNum.Text), ddlType.SelectedItem.Text, ddlVillage.SelectedItem.Text,
                    Util.ReturnSqlValue(tbAdd.Text), Util.ReturnSqlValue(tbSchool.Text), Util.ReturnSqlValue(tbMoney.Text),
                  Util.ReturnSqlValue(tbYear.Text), ddlHalf.SelectedValue);

            SqlHelper.Instance.ExecuteNonQuery(Provider, sb.ToString());

        }
        public void LoadData()
        {
            ddlVillage.DataSource = BLPoverty.GetFType(Provider);
            ddlVillage.DataTextField = "name";
            ddlVillage.DataValueField = "id";
            ddlVillage.DataBind();

            
            DataTable dt = SqlHelper.Instance.ExecuteDataTable(Provider, @"SELECT *  FROM [SStudy] where id=" + RecordID);
            if (dt.Rows.Count > 0)
            {
                LinkButton lbtnSave = Page.Master.FindControl("ToolBarContentPlaceHolder").FindControl("lbtnSave") as LinkButton;

                lbtnSave.Text = "<span ><img src=\"../Images/Icon/Action/save.gif\" alt=\"保存\" /></span><span>保存</span>";
                Page.Title = "扶贫帮困资金编辑";
                DataRow dr = dt.Rows[0];
                tbYear.Text = dr["Year"].ToString();
                tbName.Text = dr["Name"].ToString();
                tbAdd.Text = dr["address"].ToString();
                tbSchool.Text = dr["School"].ToString();
                tbIDNum.Text = dr["IDNum"].ToString();
                tbMoney.Text = dr["Money"].ToString();
                tbStuName.Text = dr["StudentName"].ToString();
                ListItem li = ddlVillage.Items.FindByText(dr["BelongTo"].ToString());
                if (li != null)
                    ddlVillage.SelectedValue = li.Value;

                li = ddlType.Items.FindByText(dr["Household"].ToString());
                if (li != null)
                    ddlType.SelectedValue = li.Value;
                li = ddlGendar.Items.FindByText(dr["gender"].ToString());
                if (li != null)
                    ddlGendar.SelectedValue = li.Value;
                li = ddlHalf.Items.FindByText(dr["halfyear"].ToString());
                if (li != null)
                    ddlHalf.SelectedValue = li.Value;

            }
        }
    }
}